package com.cqut.controller;


import com.cqut.VO.task.StudentTaskListVO;
import com.cqut.VO.task.TeacherTaskListVO;
import com.cqut.entity.CheckTask;
import com.cqut.entity.Task;
import com.cqut.service.TaskService;
import com.cqut.util.ResultVOUtil;
import com.cqut.VO.result.ResultVO;
import com.cqut.VO.task.TaskListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * <p>
 * 作业标题 前端控制器
 * </p>
 *
 * @author 冯永龙
 * @since 2021-06-15
 */
@RestController
@RequestMapping("//task")
public class TaskController {
    @Autowired
    private TaskService taskService;

    @PostMapping("/create")
    public ResultVO<Object> createTask(@RequestBody Task task) {
        if (taskService.createTask(task)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @GetMapping("/findTeacherTask/{courseId}")
    public ResultVO<TaskListVO> findTeacherTaskByCourseId(@PathVariable("courseId") Integer courseId) {
        List<TeacherTaskListVO> teacherTaskList = taskService.findTeacherTaskList(courseId);
        if (teacherTaskList != null) {
            return ResultVOUtil.success(teacherTaskList);
        }
        return ResultVOUtil.fail();
    }

    @PutMapping("/update")
    public ResultVO<Object> updateTask(@RequestBody Task task) {
        if (taskService.updateTask(task)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @DeleteMapping("/delete/{taskId}")
    public ResultVO<Object> deleteTask(@PathVariable("taskId") String taskId) {
        if (taskService.deleteTask(taskId)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @PostMapping("/submit")
    public ResultVO<Object> submitTask(@RequestBody CheckTask task) {
        if (taskService.submitTask(task)) {
            return ResultVOUtil.success(null);
        }
        return ResultVOUtil.fail();
    }

    @GetMapping("/findStudentTask/{courseId}/{userId}")
    public ResultVO<StudentTaskListVO> findStudentTask(@PathVariable("courseId") Integer courseId, @PathVariable("userId") Integer userId) {
        List<StudentTaskListVO> studentTaskList = taskService.findStudentTaskList(courseId, userId);
        if (studentTaskList != null) {
            return ResultVOUtil.success(studentTaskList);
        }
        return ResultVOUtil.fail();
    }

    @GetMapping("/getTaskInfo/{taskId}")
    public ResultVO<Task> getTaskInfo(@PathVariable("taskId") String taskId) {
        Task task = taskService.getTaskInfo(taskId);
        if (task != null) {
            return ResultVOUtil.success(task);
        }
        return ResultVOUtil.fail();
    }
}

